package com.linfeng.business.admin.executor.admin.cmd;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.linfeng.business.admin.dto.data.admin.SysAdminDTO;
import com.linfeng.business.admin.dto.event.admin.cmd.AdminLoginCmd;
import com.linfeng.business.admin.executor.admin.AdminBaseExe;
import com.linfeng.component.common.exception.CustomException;
import com.linfeng.component.common.exception.CustomExceptionCode;
import com.linfeng.component.common.tools.PasswordTools;
import com.linfeng.component.common.tools.TokenTools;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class AdminLoginCmdExe extends AdminBaseExe {

    public String execute(AdminLoginCmd adminLoginCmd) {

        SysAdminDTO adminDTO = sysAdminEntity.getByLoginName(adminLoginCmd.getLoginName());

//        SecureUtil.aes()
        return Optional.ofNullable(adminDTO)
                .filter(admin -> PasswordTools.encode(adminLoginCmd.getPassword()).equals(admin.getPassword()))
                .map(admin -> TokenTools.createToken(admin.getId()))
                .orElseThrow(() -> new CustomException(CustomExceptionCode.LOGIN_FAIL));
    }

    public static void main(String[] args) {

        AES aes = SecureUtil.aes();
        String encrypt = aes.encryptHex("123456");

        System.out.println(encrypt);
    }
}
